﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using deezer.sdk.windowsphone.Helpers;

namespace deezer.sdk.windowsphone
{
    public delegate void GetAlbumByIdResponseEventHandler(object sender, EventArgsAlbum e);
    public delegate void GetCommentsAlbumByIdResponseEventHandler(object sender, EventArgsCommentsAlbum e);
    public delegate void GetFansAlbumByIdResponseEventHandler(object sender, EventArgsFansAlbum e);
    public delegate void GetTracksAlbumByIdResponseEventHandler(object sender, EventArgsTracksAlbum e);

    public class Album
    {
        HttpWebRequest request;

        public event GetAlbumByIdResponseEventHandler GetAlbumByIdResponse;
        public event GetCommentsAlbumByIdResponseEventHandler GetCommentsAlbumByIdResponse;
        public event GetFansAlbumByIdResponseEventHandler GetFansAlbumByIdResponse;
        public event GetTracksAlbumByIdResponseEventHandler GetTracksAlbumByIdResponse;

        protected virtual void GetAlbumByIdResponseCheck(EventArgsAlbum e)
        {
            if (GetAlbumByIdResponse != null)
                GetAlbumByIdResponse(this, e);
        }

        protected virtual void GetCommentsAlbumByIdResponseCheck(EventArgsCommentsAlbum e)
        {
            if (GetCommentsAlbumByIdResponse != null)
                GetCommentsAlbumByIdResponse(this, e);
        }

        protected virtual void GetFansAlbumByIdResponseCheck(EventArgsFansAlbum e)
        {
            if (GetFansAlbumByIdResponse != null)
                GetFansAlbumByIdResponse(this, e);
        }

        protected virtual void GetTracksAlbumByIdResponseCheck(EventArgsTracksAlbum e)
        {
            if (GetTracksAlbumByIdResponse != null)
                GetTracksAlbumByIdResponse(this, e);
        }

        public void GetAlbumByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/album/{0}&output=xml",id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseIntern), null);
        }

        public void GetCommentsAlbumByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/album/{0}/comments&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseCommentsIntern), null);
        }

        public void GetFansAlbumByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/album/{0}/fans&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseFansIntern), null);
        }

        public void GetTracksAlbumByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/album/{0}/tracks&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseTracksIntern), null);
        }

        private void ByIdResponseIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsAlbum e = new EventArgsAlbum(Converter.ConvertJsonToTypeAlbum(responseStream.ReadToEnd()));
                        GetAlbumByIdResponseCheck(e);
                    }
                }
            }
        }

        private void ByIdResponseCommentsIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsCommentsAlbum e = new EventArgsCommentsAlbum(Converter.ConvertJsonToTypeComments(responseStream.ReadToEnd()));
                        GetCommentsAlbumByIdResponseCheck(e);
                    }
                }
            }
        }

        private void ByIdResponseFansIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsFansAlbum e = new EventArgsFansAlbum(Converter.ConvertJsonToTypeFans(responseStream.ReadToEnd()));
                        GetFansAlbumByIdResponseCheck(e);
                    }
                }
            }
        }

        private void ByIdResponseTracksIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsTracksAlbum e = new EventArgsTracksAlbum(Converter.ConvertJsonToTypeTracks(responseStream.ReadToEnd()));
                        GetTracksAlbumByIdResponseCheck(e);
                    }
                }
            }
        }

        
    }

    
    
}
